from django.db import models
from django.contrib.auth.models import User
from django.utils import timezone


class Task(models.Model):
    id = models.AutoField(primary_key=True)
    title = models.CharField(max_length=100, verbose_name='任务名称')
    points = models.IntegerField(default=0, verbose_name='积分奖励')
    target = models.FloatField(default=1, verbose_name='目标值')
    daily = models.BooleanField(default=True, verbose_name='是否每日任务')
    active = models.BooleanField(default=True, verbose_name='是否启用')
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    class Meta:
        db_table = 'xiangsu_tasks'
        verbose_name = '任务'
        verbose_name_plural = '任务'

    def __str__(self):
        return self.title


class TaskAssignment(models.Model):
    id = models.AutoField(primary_key=True)
    date = models.DateField(default=timezone.localdate, db_index=True, verbose_name='日期')
    task = models.ForeignKey(Task, on_delete=models.CASCADE, related_name='assignments')
    created_at = models.DateTimeField(auto_now_add=True)

    class Meta:
        db_table = 'xiangsu_task_assignments'
        unique_together = ('date', 'task')
        verbose_name = '任务发布'
        verbose_name_plural = '任务发布'


class TaskCompletion(models.Model):
    id = models.AutoField(primary_key=True)
    user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='task_completions')
    task = models.ForeignKey(Task, on_delete=models.CASCADE, related_name='completions')
    date = models.DateField(default=timezone.localdate, db_index=True)
    created_at = models.DateTimeField(auto_now_add=True)

    class Meta:
        db_table = 'xiangsu_task_completions'
        unique_together = ('user', 'task', 'date')
        verbose_name = '任务完成'
        verbose_name_plural = '任务完成'

